package com.hg.proxy;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * cglib代理
 * created by skh on 2019/9/7
 */
public class CglibProxy implements MethodInterceptor {
	@Override
	public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
		before();
		Object result = methodProxy.invokeSuper(o, objects);
		after();
		return result;
	}

	public Object getProxy(Class cls) {
		return Enhancer.create(cls, this);
	}

	private void before() {
		System.out.println("前置增强");
	}

	private void after() {
		System.out.println("后置增强");
	}

	public static void main(String[] args) {
		Hello proxy = (Hello)new CglibProxy().getProxy(HelloImpl.class);
		proxy.sayHello();
	}
}
